//IO函子
const fs = require('fs')
const fp = require('lodash/fp')
class IO {
    static of (value){
        return new IO(function (){
            return value
        })
    }

    constructor (fn){
        this._value = fn
    }

    map (fn){
        return new IO(fp.flowRight(fn,this._value))
    }

    join (){
        return this._value()
    }

    flatMap (fn){
        return this.map(fn).join()
    }
}

let readFile = function (filename){
    return IO.of(filename).map(f => fs.readFileSync(f,'utf-8'))
}

let print = function (x){
    return IO.of(x).map(x => {
        //console.log(x)
        return x
    })
}

let r = readFile('package.json')
        .flatMap(print)
        .join()
console.log(r)

// let cat = fp.flowRight(print, readFile)
// let r = cat('package.json')._value()._value()
// console.log(r)

// let r = IO.of(process).map(p => p.execPath)
// console.log(r._value())